/*
 * @Author: Du.M
 * @Date: 2021-07-05 10:52:26
 * @LastEditTime: 2021-08-04 14:29:38
 * @FilePath: \express_basics\service\tableService.js
 */
const { querySql } = require("../utils")
const { getToken,decode }  = require("../utils/user-jwt")
const multiparty = require('multiparty');

const getTableList = (req,res,next) =>{
    const sql = `select a.*,b.* from (SELECT s.* from house s)a  left join (select c.* from rend_people c)b ON a.id = b.house_id and b.status = 1; `
    querySql(sql).then( result =>{
        const data = {
            status:'success',
            data:result,
            total:result.length
        }
        res.status(200).json(data)
    })

}
//新增记录
const postTableList = (req,res,next) =>{
    const { body } = req
    console.log(body)
    const form = new multiparty.Form();
    form.uploadDir='./public/images'
    form.parse(req, function(err, fields, files) {
        if (err) {
        } else {
            const {name,phone,address,house_id,house_price,water_price,in_date,remark} = fields
            console.log('remark',remark)
            const id_card_forward = files.picture[0].path
            const newString = id_card_forward.replace(/\\/g, "/")
            const sql = `insert into rend_people(status,name,phone,address,house_id,house_price,water_price,id_card_forward,in_date,last_pay_date,remark) values (1,"${name}",${phone},"${address}",${house_id},${house_price},${water_price},"${newString}","${in_date}","${in_date}","${remark == 'undefined' ? '-' : remark}")`
            querySql(sql).then(result =>{
                res.status(200).json({
                    status:'200'
                })
            })
        }
    });
    
}

const putTableList = (req,res,next) =>{
    const {body} = req
    const {pay_to_date,people_id,house_price,water_price,electricity_price,all_price,pay_date,remark} = body
    console.log(electricity_price)
    const sql_update = `update rend_people set last_pay_date = "${pay_to_date}" , remark = "${remark == 'undefined' ? '-' : remark}" where people_id = ${people_id}`
    const sql_insert = `insert into pay_record(people_id,house_price,water_price,electricity_price,all_price,pay_date) value (${people_id},"${house_price}","${water_price}","${electricity_price}","${all_price}","${pay_date}")`
    querySql(sql_update).then(result =>{
        console.log('第一步成功没')
        querySql(sql_insert).then(result =>{
            console.log('第二步成功没')
            res.status(200).json({
                status:'200'
            })
        })
    })
}
//获取空房
const getHouseTree = (req,res,next) =>{
    const sql = 'select a.*,b.* from (SELECT s.* from house s)a  left join (select c.* from rend_people c)b ON a.id = b.house_id and b.status = 1;'
    querySql(sql).then(result =>{
        const arr = []
        result.forEach(ele =>{
            if(ele.status != 1){
                const item = {
                    key:ele.id,
                    label:ele.house_name,
                    value:ele.id,
                    electricity:ele.electricity
                }
                arr.push(item)
            }
        })
        res.status(200).json(arr)
    })

}
//人员搬出
const deleteTableList = (req,res,next) =>{
    const {params:id} = req
    const sql = `update rend_people set status = 0 where people_id = ${id.id}`
    querySql(sql).then(result =>{
        res.status(200).json({
            status:200
        })
    })
}
module.exports = {
    getTableList,
    postTableList,
    getHouseTree,
    deleteTableList,
    putTableList
}